package com.gxa.controller;

import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Activity;
import com.gxa.pojo.FileActivity;
import com.gxa.service.ActivityService;
import com.gxa.utils.GenerateSequenceUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@Api(value = "活动数据test",tags = "活动数据接口API")
@RequestMapping(value = "/activity",method = RequestMethod.GET)
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    // 查询活动信息
    @RequestMapping(value = "/queryActivityList", method = RequestMethod.GET)
    @ApiOperation(value = "活动查询接口", notes = "传递当前页码数和每页显示记录数", httpMethod = "GET", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData queryActivityList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize) {

        return activityService.queryActivityList(currentPage, pageSize);
    }

    // 添加活动
    @RequestMapping(value = "/addActivity", method = RequestMethod.POST)
    @ApiOperation(value = "添加活动接口", notes = "活动信息", httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData addActivity(@ApiParam(name = "fileActivity", value = "添加活动", required = true) FileActivity fileActivity, HttpServletRequest request) throws IOException {
        ResponseData rd = new ResponseData();
        if (fileActivity.getActivity_pic() == null || fileActivity.getActivity_pic().isEmpty()) {
            rd.setCode(500);
            rd.setMsg("没有选中文件");
            return rd;
        } else {//正常上传
            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            MultipartFile multipartFile = fileActivity.getActivity_pic();
            String fileName = multipartFile.getOriginalFilename();
            //截取文件后缀
            String ext = fileName.substring(fileName.lastIndexOf("."));
            //生成时间戳作为文件名
            String fileName1 = GenerateSequenceUtil.generateSequenceNo();
            //上传文件名称：不能用原名称，需要生成一个唯一的名字2021102911282500140.jpg
            String newFileName = fileName1 + ext;
            //将图片的相对路径保存下来，传递给数据库。
            String fileDataBasePath = File.separator + "upload" + File.separator + newFileName;
            File newFile = new File(uploadPath + newFileName);
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(), newFile);

            Activity activity = new Activity();
            activity.setActivity_id(fileActivity.getActivity_id());
            activity.setActivity_theme(fileActivity.getActivity_theme());
            activity.setActivity_time(fileActivity.getActivity_time());
            activity.setActivity_content(fileActivity.getActivity_content());
            activity.setActivity_pic(fileDataBasePath);
            // 调用service
            return activityService.addActivity(activity);
        }

    }

    // 修改活动信息
    @RequestMapping(value = "/updateActivity", method = RequestMethod.POST)
    @ApiOperation(value = "修改活动接口", notes = "修改活动信息", httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData updateActivity(@ApiParam(name = "fileActivity", value = "修改活动信息", required = true) FileActivity fileActivity, HttpServletRequest request) throws IOException {
        ResponseData rd = new ResponseData();
        Activity activity = new Activity();
        activity.setActivity_id(fileActivity.getActivity_id());
        activity.setActivity_theme(fileActivity.getActivity_theme());
        activity.setActivity_time(fileActivity.getActivity_time());
        activity.setActivity_content(fileActivity.getActivity_content());
        if (fileActivity.getActivity_pic() == null || fileActivity.getActivity_pic().isEmpty()) {

            return activityService.updateActivityNoFile(activity);
        } else {//正常上传
            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
            File file = new File(uploadPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            MultipartFile multipartFile = fileActivity.getActivity_pic();
            String fileName = multipartFile.getOriginalFilename();
            //截取文件后缀
            String ext = fileName.substring(fileName.lastIndexOf("."));
            //生成时间戳作为文件名
            String fileName1 = GenerateSequenceUtil.generateSequenceNo();
            //上传文件名称：不能用原名称，需要生成一个唯一的名字2021102911282500140.jpg
            String newFileName = fileName1 + ext;
            //将图片的相对路径保存下来，传递给数据库。
            String fileDataBasePath = File.separator + "upload" + File.separator + newFileName;
            File newFile = new File(uploadPath + newFileName);
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(), newFile);
            activity.setActivity_pic(fileDataBasePath);
            // 调用service
            return activityService.updateActivity(activity);
        }
    }

    // 回显编辑活动信息
    @RequestMapping(value = "/editActivity", method = RequestMethod.GET)
    @ApiOperation(value = "回显编辑活动接口", notes = "回显编辑活动信息", httpMethod = "GET", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData editActivity(@RequestBody @ApiParam(name = "activity", value = "活动信息", required = true) Activity activity) {

        Integer activity_id = activity.getActivity_id();
        return activityService.editActivity(activity_id);
    }

    // 删除活动
    @RequestMapping(value = "/delActivity", method = RequestMethod.POST)
    @ApiOperation(value = "删除活动接口", notes = "删除活动信息", httpMethod = "POST", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData delActivity(@RequestBody @ApiParam(name = "activity_id", value = "活动信息", required = true) Activity activity) {

        Integer activity_id = activity.getActivity_id();
        return activityService.delActivity(activity_id);
    }

    //关键字查找
    @RequestMapping(value = "/searchActivityList", method = RequestMethod.GET)
    @ApiOperation(value = "活动关键字查找接口", notes = "传递当前页码数和每页显示记录数", httpMethod = "GET", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页码数", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", required = true, dataType = "String", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @ResponseBody
    public ResponseData searchActivityList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("keyword") String keyword) {

        return activityService.searchActivityList(currentPage, pageSize, keyword);
    }

}